<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    chartData: {
      type: Number,
      required: true
    },
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    // chartData: {
    //   deep: true,
    //   handler(val) {
    //     this.setOptions(val)
    //   }
    // }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOption(){
      this.chart.setOption({
        title:{
          text:"CPU利用率",
          bottom: 10,
          left: 'center',
        },
        tooltip: {
            formatter: '{a} <br/>{b} : {c}%'
          },
          series: [
            {
              name: 'CPU利用率',
              type: 'gauge',
              detail: {formatter: '{value}%'},
              data: [{value: this.chartData, 
                      name: 'CPU'}]
            }
        ]
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOption()
      // setInterval(this.setOption(),1000);
    }
  }
}
</script>
